package com.gxa.modules.payment.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.modules.payment.form.MeterForm;
import com.gxa.modules.payment.util.Result;
import com.gxa.modules.payment.entity.Meter;
import com.gxa.modules.payment.service.MeterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "仪表管理 接口")
@RestController
public class MeterController {

    @Autowired
    private MeterService meterService;

    @ApiOperation("仪表管理 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前第几页"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少数据"),
            @ApiImplicitParam(paramType = "query",name = "meterName", value = "查询条件 仪表名称"),
    })
    @GetMapping("/meter/list")
    public Result<List<Meter>> listResult(@RequestParam @ApiIgnore Map<String,Object> params){
        Result<List<Meter>> r = Result.success();

        String page1 = (String) params.get("page");
        String limit1 = (String) params.get("limit");

        Integer page = Integer.parseInt(page1);
        Integer limit = Integer.parseInt(limit1);

        try{
            PageHelper.startPage(page,limit);
            List<Meter> meters = this.meterService.queryByPage(params);
            PageInfo<Meter> pageInfo = new PageInfo<>(meters);

            long total = (int)pageInfo.getTotal();
            return Result.success(meters,total);
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

//    @ApiOperation("生成 仪表编号")
//    @GetMapping("/meter/addNo")
//    public Result addNo(){
//        Result r = Result.failed();
//        try{
//            Integer receiptNo = this.meterService.addNo();
//            r = Result.success(receiptNo);
//        } catch (Exception e){
//            r.setCode("1");
//            r.setMsg("error");
//            e.printStackTrace();
//        }
//        return r;
//    }

    @ApiOperation("仪表管理 添加")
    @PostMapping("/meter/add")
    public Result addMeter(@RequestBody MeterForm meterForm){
        System.out.println(meterForm);
        Result r = new Result();

        try{
            this.meterService.meterAdd(meterForm);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("仪表管理 修改前查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "code", value = "查询条件 仪表编号"),
    })
    @GetMapping("/meter/queryByCode")
    public Result<List<Meter>> queryByCode(@RequestParam Integer code){
        Result<List<Meter>> r = Result.success();

        try{
            List<Meter> meter = this.meterService.queryAll(code);
            PageInfo<Meter> pageInfo = new PageInfo<>(meter);

            long total = (int)pageInfo.getTotal();
            return Result.success(meter,total);
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("仪表管理 修改")
    @PutMapping("/meter/update")
    public Result meteUpdate(@RequestBody MeterForm meterForm){
        System.out.println(meterForm);
        Result r = new Result();

        try{
            this.meterService.meterUpdate(meterForm);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("仪表管理 删除")
    @DeleteMapping("/meter/delete")
    public Result meteDelete(@RequestParam Integer code){
        Result r = new Result();

        try{
            this.meterService.meterDelete(code);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("仪表管理 删除多条")
    @DeleteMapping("/meter/ids/delete")
    public Result meteDelete(@RequestBody List<Integer> codes){
        Result r = new Result();

        try{
            this.meterService.meterDeleteIds(codes);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
}
